import Vue from 'vue'
import { login, getMenu, logout, updatePass } from '@/api/login'
import { ACCESS_TOKEN, ACCESS_COOKIES } from '@/store/mutation-types'
import { welcome } from '@/utils/util'

const user = {
  state: {
    token: '',
    name: '',
    welcome: '',
    avatar: '',
    roles: [],
    roleId: '',
    info: {},
    IsSpecial: false,
    UserLevel: '',
    DepartmentID: ''
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { name, welcome }) => {
      state.name = name
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    },
    SET_ROLE: (state, roleId) => {
      state.roleId = roleId
    },
    SET_IDTOKEN: (state, idenToken) => {
      state.idenToken = idenToken
    },
    SET_SPECIAL: (state, IsSpecial) => {
      state.IsSpecial = IsSpecial
    },
    SET_USERLEVEL: (state, UserLevel) => {
      state.UserLevel = UserLevel
    },
    SET_DEPARTMENTID: (state, DepartmentID) => {
      state.DepartmentID = DepartmentID
    }
  },

  actions: {
    // 登录
    Login ({ commit }, token) {
      const _token = Object.assign(token.User, { token: token.Token, RoleID: token.RoleID, DepartmentCode: token.DepartmentCode, DepartmentName: token.DepartmentName, DepartmentID: token.DepartmentID,IsSpecial: token.IsSpecial, UserLevel: token.UserLevel })
      Vue.ls.set(ACCESS_TOKEN, _token, 7 * 24 * 60 * 60 * 1000)
      window.localStorage.setItem('RoleID', token.RoleID);
      window.localStorage.setItem('UserLevel', token.UserLevel);
      window.localStorage.setItem('DepartmentID', token.DepartmentID);
      commit('SET_TOKEN', _token)
      commit('SET_NAME', {
        name: token.User.UserName
      })
      commit('SET_SPECIAL', token.IsSpecial)
      commit('SET_USERLEVEL', token.UserLevel)
      commit('SET_DEPARTMENTID', token.DepartmentID)
      commit('SET_ROLE', token.RoleID)
      commit('SET_IDTOKEN', token.Token)

    },

    // 获取用户信息
    GetInfo ({ commit }, token) {
      return new Promise((resolve, reject) => {
        login()
          .then(response => {
            const result = response.data.children
            if (result.length > 0) {
              const permissions = []
              result.data = permissions
              commit('SET_ROLES', permissions)
            } else {
              reject(new Error('getInfo: roles must be a non-null array !'))
            }
            resolve(token)
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    updatePassword ({ commit }, value) {
      updatePass(value).then(res => { })
    },
    // 登出
    Logout ({ commit, state }) {
      return new Promise(resolve => {
        logout(state.token)
          .then(() => {
            resolve()
          })
          .catch(() => {
            resolve()
          })
          .finally(() => {
            commit('SET_TOKEN', '')
            commit('SET_ROLES', [])
            Vue.ls.remove(ACCESS_TOKEN)
            Vue.ls.set('fromlogin', true)
          })
      })
    }
  }
}

export default user
